<?php
ini_set('memory_limit', '-1');
ini_set('max_execution_time', 86400);
           
// Set flag that this is a parent file.
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);

if (file_exists(dirname(__FILE__) . '/defines.php')) {
    include_once dirname(__FILE__) . '/defines.php';
}

if (!defined('_JDEFINES')) {
    define('JPATH_BASE', dirname(__FILE__));
    require_once JPATH_BASE.'/includes/defines.php';
}

require_once JPATH_BASE.'/includes/framework.php';

// Instantiate the application.
$app = JFactory::getApplication('site');

// Initialise the application.
$app->initialise();

require_once JPATH_BASE.DS.'administrator'.DS.'components'.DS.'com_wrd'.DS.'classes'.DS.'utilities'.DS.'reportProvider.php';
require_once JPATH_BASE.DS.'administrator'.DS.'components'.DS.'com_wrd'.DS.'classes'.DS.'utilities'.DS.'pointProvider.php';
require_once JPATH_BASE.DS.'administrator'.DS.'components'.DS.'com_wrd'.DS.'classes'.DS.'PHPExcel.php';

// Init excel
$objPHPExcel = new PHPExcel();

// Set properties
$objPHPExcel->getProperties()->setCreator("Wmc")
                             ->setLastModifiedBy("Wmc")
                             ->setTitle("Redemption Report")
                             ->setSubject("Redemption Report")
                             ->setDescription("Redemption Report")
                             ->setKeywords("Redemption Report")
                             ->setCategory("Redemption Report");
// Create a first sheet, representing sales data
$objPHPExcel->setActiveSheetIndex(0);
$activeSheet = $objPHPExcel->getActiveSheet();
$activeSheet->mergeCells('A1:L1');
$activeSheet->setCellValue('A1', 'Redemption Report');
$activeSheet->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$activeSheet->getStyle('A1')->getFont()->setSize(24);
$activeSheet->getStyle('A1')->getFont()->setBold(true);

/************** FILL HEADER **************/
$index = 5;
$activeSheet->setCellValue('A'.$index, 'Code');
$activeSheet->setCellValue('B'.$index, 'Member ID');
$activeSheet->setCellValue('C'.$index, 'Name');
$activeSheet->setCellValue('D'.$index, 'Phone');
$activeSheet->setCellValue('E'.$index, 'Gift Name');
$activeSheet->setCellValue('F'.$index, 'Quantity');
$activeSheet->setCellValue('G'.$index, 'Points');
$activeSheet->setCellValue('H'.$index, 'Cash');
$activeSheet->setCellValue('I'.$index, 'Outlet');
$activeSheet->setCellValue('J'.$index, 'Delivery');
$activeSheet->setCellValue('K'.$index, 'Pick Up Date');
$activeSheet->setCellValue('L'.$index, 'Status');

$cols = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L');

foreach($cols as $item)
{
    if($item == 'C')
    {
        setBackgroundColor($activeSheet, $item, 5, 35);
    }
    else if($item == 'I')
    {
        setBackgroundColor($activeSheet, $item, 5, 40);
    }
    else if($item == 'E')
    {
        setBackgroundColor($activeSheet, $item, 5, 28);
    }
    else
    {
        setBackgroundColor($activeSheet, $item, 5, 18);   
    }
}

// End init excel

/******** Query Data ********/  
$db = JFactory::getDbo();
$conditions = array();
$conditions['dateFrom'] = strval($_GET['df']);
$conditions['dateTo'] = strval($_GET['dt']);

$conditions['filter_field'] = strval($_GET['ff']);
$conditions['search_value'] = strval($_GET['sv']);
$conditions['filter'] = strval($_GET['f']);

$conditions['status'] = strval($_GET['s']);
$conditions['products'] = strval($_GET['p']);
$conditions['outlet'] = strval($_GET['o']);
//echo '<pre>';print_r($conditions);die;
$data = reportProvider::redemptionreport($conditions, $db, intval($_GET['l']));

//echo '<pre>';print_r($data);die;
if(!empty($data))
{
    $activeSheet->mergeCells('A3:L3');
    $activeSheet->setCellValue('A3', 'Total: '.$data['total'].'. Display top: '.count($data['items']));   
    $style = $activeSheet->getStyle('A3')->getFont();
    $style->setSize(16);
    $style->setBold(true);
    $style->setColor( new PHPExcel_Style_Color( 'CC0000' ) );
    
    /*********** BIND TO EXCEL ***********/
    $index = 6;
    $data = $data['items'];
    $size = count($data);
    $approve = array(
        '0' => 'Waiting',
        '1' => 'Approve',
        '2' => 'UnApprove'
    );
    
    for($i=0; $i<$size; $i++)
    {
        $activeSheet->setCellValue('A'.$index, 'V'.$data[$i]->code); 
        
        $activeSheet->setCellValue('B'.$index, $data[$i]->member_id);    
        $activeSheet->getStyle('B'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        
        $activeSheet->setCellValue('C'.$index, $data[$i]->full_name);                
        
                     
        $activeSheet->setCellValue('D'.$index, $data[$i]->phone);                
        $activeSheet->getStyle('D'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        
        $data[$i]->product = unserialize($data[$i]->product);
        $data[$i]->product = $data[$i]->product['en-GB'];
        
        $activeSheet->setCellValue('E'.$index, $data[$i]->product);
        
        $activeSheet->setCellValue('F'.$index, $data[$i]->quantity);   
        $activeSheet->getStyle('F'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
                 
        $activeSheet->setCellValue('G'.$index, (intval($data[$i]->point) + intval($data[$i]->pointx)));
        $activeSheet->getStyle('G'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $activeSheet->setCellValue('H'.$index, $data[$i]->cash);           
        $activeSheet->getStyle('H'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        
        
        $activeSheet->setCellValue('I'.$index, $data[$i]->outlet);  
        $activeSheet->setCellValue('J'.$index, $data[$i]->status_of_redemption_date);            
        $activeSheet->getStyle('J'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        
        $activeSheet->setCellValue('K'.$index, $data[$i]->pick_up_date);            
        $activeSheet->getStyle('K'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        
        $activeSheet->setCellValue('L'.$index, $approve[$data[$i]->approved]);            
        $activeSheet->getStyle('L'.$index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        
        $index++;
    }
    unset($data);
   
}

$fileName = 'redemption_report_'.date('m-d-Y');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(JPATH_BASE.DS.'tmp'.DS.$fileName.'.xlsx');

$fullPath = JPATH_ROOT.DS.'tmp'.DS.$fileName.'.xlsx';
$fsize = filesize($fullPath);

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$fileName.'.xlsx"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $fsize);
ob_clean();
flush();
readfile( $fullPath );
exit;          


function setBackgroundColor(&$activeSheet, $colName, $colIndex, $width=15)
{
    $fill = $activeSheet->getStyle($colName.$colIndex)->getFill();
    $fill->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
    $fill->getStartColor()->setARGB('cc99ff');  
    $activeSheet->getColumnDimension($colName)->setWidth($width);    
    
    $style = $activeSheet->getStyle($colName.$colIndex);
    $style->getFont()->setSize(12);
    $style->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
}
